package org.yxing.palpalgps.activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.yxing.palpalgps.AbstractActivity;
import org.yxing.palpalgps.R;
import org.yxing.palpalgps.domain.AsyncImageLoader;
import org.yxing.palpalgps.http.AsyncRequest;
import org.yxing.palpalgps.http.AsyncRequest.FetchListener;
import org.yxing.palpalgps.oauth.OAuth;
import org.yxing.palpalgps.utils.Helper;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class InterestSelectOneActivity extends AbstractActivity implements OnItemClickListener {
    
    public static final String TAG = "InterestSelectOneActivity";
    
    private Adapter mAdapter;
    
    private ListView list;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.interest_select_one);
        setTitle(getString(R.string.select_interest));
        
        list = (ListView)findViewById(android.R.id.list);
        list.setOnItemClickListener(this);
        
        mAdapter = new Adapter();
        list.setAdapter(mAdapter);
        
        mAdapter.getData();
    }
    
    private class Adapter extends BaseAdapter {
        
        private ArrayList<JSONObject> data;
        
        public Adapter() {
            data = new ArrayList<JSONObject>();
        }

        public int getCount() {
            return data.size();
        }

        public JSONObject getItem(int position) {
            return data.get(position);
        }

        public long getItemId(int position) {
            return (long)position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            JSONObject json = getItem(position);
            convertView = getLayoutInflater().inflate(R.layout.interest_select_one_item, null);
            convertView.setBackgroundDrawable(getResources().getDrawable(R.drawable.list_bg));
            TextView nameView = (TextView) convertView.findViewById(R.id.name);
            ImageView iconView = (ImageView) convertView.findViewById(R.id.icon);
            
            AsyncImageLoader.ImageCallback callback = new AsyncImageLoader.ImageCallback() {
                
                public void imageLoaded(Drawable imageDrawable, ImageView imageView,
                        String imageUrl) {
                    // TODO Auto-generated method stub
                    imageView.setImageDrawable(imageDrawable);
                }
            };
            
            AsyncImageLoader asyncImageLoader = new AsyncImageLoader();
            
            try {
                nameView.setText(json.getString("name"));
                asyncImageLoader.loadDrawable(getImgSrc(json.getString("id")), iconView, callback);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return convertView;
        }
        
        public void setItem(JSONObject json) {
            data.add(json);
            notifyDataSetChanged();
        }
        
        public void getData() {
            final List<BasicNameValuePair> params = OAuth.createAuthParams();
            params.add(new BasicNameValuePair("pid", "0"));
            AsyncRequest ar = new AsyncRequest(InterestSelectOneActivity.this);
            ar.fetch("group/list", params, new FetchListener() {
                
                public void onError(int code, String msg) {
                    
                }
                
                public void onComplete(JSONObject response) {
                    try {
                        JSONArray result = response.getJSONArray("result");
                        for (int i = 0; i < result.length(); i ++) {
                            JSONObject data = result.getJSONObject(i);
                            setItem(data);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        
        private String getImgSrc(String id) {
            return "http://" + Helper.getHost() + "/images/interest/" +  id + ".png";
        }
        
    }

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        try {
            String iid = mAdapter.getItem(position).getString("id");
            Intent intent = new Intent();
            intent.setClass(this, InterestSelectTwoActivity.class);
            intent.putExtra("iid", iid);
            startActivity(intent);
//            Log.v(TAG, iid);
        } catch (JSONException e) {
            
        }
    }
    
}
